//
//  ByImageNewsVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/30.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByImageNewsVC.h"

/** 单元格 */
#import "BySingalImageNewsCell.h"       // cell

/** api */
#import "ByNetNewsApi.h"

#import "ByNetNewsModel.h"


@interface ByImageNewsVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSMutableArray *imageNewsAry;         // 图片新闻数组

@end

@implementation ByImageNewsVC

- (NSMutableArray *)imageNewsAry {
    if (!_imageNewsAry) {
        _imageNewsAry = [[NSMutableArray alloc] init];
    }
    return _imageNewsAry;
}

#pragma mark - viewController method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"图片新闻";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBackBarButtonItem];
    
    [self createCollectionView];
    
    [self postQueryForImageNews];
}

#pragma mark - create view
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    CGFloat width = (SCREEN_WIDTH - 10 * 3) / 2;
    CGFloat height = width + 30;
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[BySingalImageNewsCell class] forCellWithReuseIdentifier:@"BySingalImageNewsCell"];
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageNewsAry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BySingalImageNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BySingalImageNewsCell" forIndexPath:indexPath];
    
    ByNetNewsItemModel *model = self.imageNewsAry[indexPath.item];
    cell.contentLabel.text = model.post_title;
    [cell.imageView setImageWithURL:model.main_img placeholderImage:DEFAULT_IMAGE];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"tap");
}


/** 获取图片新闻 */
- (void)postQueryForImageNews {
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_IMAGE];
    params.page_num = @"1";
    params.page_size = @"6";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        
        ByNetNewsModel *model = result.data;
        NSLog(@"获取到的图片新闻的个数:%li", model.results.count);
        [self.imageNewsAry removeAllObjects];
        [self.imageNewsAry addObjectsFromArray:model.results];
        
//        NSIndexSet *set = [NSIndexSet indexSetWithIndex:3];
//        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        
        [self.collection reloadData];
        
    } failure:^(ByNetBaseResult *result) {
    }];
}

@end
